import pymysql


def exeSql(sql):
    host = 'localhost'
    user = 'root'
    passwd = ''
    database = 'test0802'
    db = pymysql.connect(host, user, passwd, database, charset='utf8')
    cursor = db.cursor()
    try:
        cursor.execute(sql)
        db.commit()  # 提交到数据库执行
        results = cursor.fetchall()  # 取得所有数据
        print(results)
        return results
    except:
        db.rollback()  # 发生错误数据回滚
    db.close()


if __name__ == '__main__':
    sql1 = 'CREATE TABLE stu (id INT(4) not NULL PRIMARY KEY auto_increment,name char(20) not null,sex int(4) not NULL DEFAULT \'0\')ENGINE=InnoDB DEFAULT CHARSET=UTF8'
    sql2 = 'INSERT INTO stu VALUES(a.txt,\'张三\',a.txt), (2, \'Sam\', 0);'
    sql3 = 'SELECT * FROM stu'
    exeSql(sql3)
